package com.leyou.controller;

import com.leyou.entity.Cart;
import com.leyou.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.controller
 * @date 2019/7/11 21:27
 * @description:
 **/
@RestController
public class CartController {


    @Autowired
    private CartService cartService;


    /**
     * 加入购物车
     * @param cart
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart){

        cartService.addCart(cart);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 查询购物车的信息
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Cart>> queryCart(){

       List<Cart> carts= cartService.queryCart();

       if (carts==null){
           return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
       }
        return ResponseEntity.ok(carts);

    }


    /**
     * 修改商品的数量
     * @param skuId
     * @param num
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateGoodsNum(
            @RequestParam("id") Long skuId,@RequestParam("num") Integer num
    ){
        cartService.updateGoodsNum(skuId,num);
        return ResponseEntity.ok().build();

    }

    /**
     * 删除商品
     * @param skuId
     * @return
     */
    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteGoods(@PathVariable("skuId")Long skuId){
        cartService.deleteGoods(skuId);
        return ResponseEntity.ok().build();
    }

    /**
     * 批量新增carts
     * @param carts
     * @return
     */
    @PostMapping("list")
    public ResponseEntity<Void> addCartList(@RequestBody List<Cart> carts){
        cartService.addCartList(carts);
        return ResponseEntity.ok().build();
    }

}
